﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using BusinessRules;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DataAccess;
using Table = CrystalDecisions.CrystalReports.Engine.Table;

public partial class ReportesGenerales : PageBase{
    #region Propiedades Publicas
    //paso de un parametro
    public ParameterField crtParamField = new ParameterField();
    public ParameterFields crtParamFields = new ParameterFields();
    public ParameterDiscreteValue paramValue = new ParameterDiscreteValue();


    
    public ParameterField crtParamField2 = new ParameterField();
    public ParameterFields crtParamFields2 = new ParameterFields();
    public ParameterDiscreteValue paramValue2 = new ParameterDiscreteValue();


    public ReportDocument rep = new ReportDocument();

    private DataTable AdmRubros
    {

        get
        {

            DataTable objTable = null;
            if (Cache["AdmRubros" + Session.SessionID] != null)
                objTable = (DataTable)Cache["AdmRubros" + Session.SessionID];
            return objTable;
        }

        set
        {
            if (value != null)
            {
                Cache.Insert("AdmRubros" + Session.SessionID, value, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
            }
        }
    }

    #endregion

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack){
            cargarRubros();
        }
        else{
            CrystalReportViewer1.ReportSource = Session["repPuntajeEstudiantesXPrograma"];
            CrystalReportViewer1.DataBind();
            CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
                if (AdmRubros != null)
                {
                    ASPxGridViewRubros.DataSource = AdmRubros;
                    ASPxGridViewRubros.DataBind();
                }}
    }

    #endregion

    #region cargarRubros

    private void cargarRubros(){
        SigPlanAccion objSigPlanAccion = new SigPlanAccion(ConfigurationManager.AppSettings["Conn"]);
        SigPlanAccionData objSigPlanAccionData = objSigPlanAccion.ConsultarPlanAccionAdministracionPer_Id(Usuario.PerfilActual.ToString());//Usuario.PerfilActual.ToString());

        if (objSigPlanAccionData != null)
        {
            ASPxGridViewRubros.DataSource = objSigPlanAccionData;
            AdmRubros = objSigPlanAccionData;
            ASPxGridViewRubros.DataBind();
        }
    }

    #endregion

    #region GeneraReporteAvancesMetas
    private void GeneraReporteAvancesMetas(string strPrgIdLocal,  string strplnAno )
    {
        ConnectionInfo connInfo = new ConnectionInfo();
        connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
        connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
        connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
        connInfo.Password = ConfigurationManager.AppSettings["Password"];
        rep.Load(Server.MapPath("Reportes\\Metas_Avances.rpt"), OpenReportMethod.OpenReportByTempCopy);
        CrystalReportViewer1.ReportSource = rep;
        Tables repTbls = rep.Database.Tables;

        paramValue.Value = strPrgIdLocal;
        crtParamField.ParameterFieldName = "@PLN_ID";
        crtParamField.CurrentValues.Add(paramValue);
        crtParamFields.Add(crtParamField);

        paramValue2.Value = strplnAno;
        crtParamField2.ParameterFieldName = "@PLN_ANO";
        crtParamField2.CurrentValues.Add(paramValue2);
        crtParamFields.Add(crtParamField2);

        foreach (CrystalDecisions.CrystalReports.Engine.Table repTbl in repTbls)
        {
            TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
            repTblLogonInfo.ConnectionInfo = connInfo;
            repTbl.ApplyLogOnInfo(repTblLogonInfo);
            repTbl.Location = "MCSIG" + ".dbo." + repTbl.Name;
        }

        Session["repPuntajeEstudiantesXPrograma"] = rep;
        CrystalReportViewer1.ReportSource = rep;
        CrystalReportViewer1.DataBind();

        CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
    } 
    #endregion
        
    #region btnVerReporteAvancesMetas_Click

    protected void btnVerReporteAvancesMetas_Click(object sender, EventArgs e)
    {
        try
        {
            GeneraReporteAvancesMetas(TraerFilasSeleccionadasDelGrid(), ddlAnos.SelectedValue);
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }
    }

    #endregion

    #region TraerFilasSeleccionadasDelGrid

    private string TraerFilasSeleccionadasDelGrid()
    {
        string strParametroReporte = "";
        ArrayList objArray = new ArrayList();
        List<object> keyValues = ASPxGridViewRubros.GetSelectedFieldValues("PLN_ID");
        try
        {
            foreach (object key in keyValues)
            {
                if (strParametroReporte == "")
                    strParametroReporte = key.ToString();
                else
                    strParametroReporte = strParametroReporte + "," + key.ToString();
            }
        }
        catch (Exception ex)
        {
            //throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", this.GetType().FullName), ex);
            lblMensaje.Text = ex.Message;
        }
        return strParametroReporte;
    }

    #endregion

    #region btnRelacionEpCdp_Click

    protected void btnRelacionEpCdp_Click(object sender, EventArgs e)
    {
        try
        {
            GeneraReporteRelacionEPyCDP(TraerFilasSeleccionadasDelGrid());
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }
    }

    #endregion

    #region GeneraReporteRelacionEPyCDP
    private void GeneraReporteRelacionEPyCDP(string strPrgIdLocal)
    {

        ConnectionInfo connInfo = new ConnectionInfo();
        connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
        connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
        connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
        connInfo.Password = ConfigurationManager.AppSettings["Password"];
        rep.Load(Server.MapPath("Reportes\\Relacion_EP_CDP.rpt"), OpenReportMethod.OpenReportByTempCopy);
        CrystalReportViewer1.ReportSource = rep;
        Tables repTbls = rep.Database.Tables;

        paramValue.Value = strPrgIdLocal;
        crtParamField.ParameterFieldName = "@PLN_ID";
        crtParamField.CurrentValues.Add(paramValue);
        crtParamFields.Add(crtParamField);

        foreach (CrystalDecisions.CrystalReports.Engine.Table repTbl in repTbls)
        {
            TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
            repTblLogonInfo.ConnectionInfo = connInfo;
            repTbl.ApplyLogOnInfo(repTblLogonInfo);
            repTbl.Location = "MCSIG" + ".dbo." + repTbl.Name;
        }
        Session["repPuntajeEstudiantesXPrograma"] = rep;
        CrystalReportViewer1.ReportSource = rep;
        CrystalReportViewer1.DataBind();
        CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
    }
    #endregion

    #region btnValorEpCdp_Click

    protected void btnValorEpCdp_Click(object sender, EventArgs e)
    {
        try
        {
            ConnectionInfo connInfo = new ConnectionInfo();
            connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
            connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
            connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
            connInfo.Password = ConfigurationManager.AppSettings["Password"];
            rep.Load(Server.MapPath("Reportes\\Valor_EP_CDP_Tarea.rpt"), OpenReportMethod.OpenReportByTempCopy);
            CrystalReportViewer1.ReportSource = rep;
            Tables repTbls = rep.Database.Tables;

            paramValue.Value = TraerFilasSeleccionadasDelGrid();
            crtParamField.ParameterFieldName = "@PLN_ID";
            crtParamField.CurrentValues.Add(paramValue);
            crtParamFields.Add(crtParamField);

            foreach (Table repTbl in repTbls)
            {
                TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
                repTblLogonInfo.ConnectionInfo = connInfo;
                repTbl.ApplyLogOnInfo(repTblLogonInfo);
                repTbl.Location = "MCSIG" + ".dbo." + repTbl.Name;
            }
            Session["repPuntajeEstudiantesXPrograma"] = rep;
            CrystalReportViewer1.ReportSource = rep;
            CrystalReportViewer1.DataBind();
            CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }
    }

    #endregion

    protected void btnEjecucionPresupuestal2012_Click(object sender, EventArgs e)
    {
        try
        {
            ConnectionInfo connInfo = new ConnectionInfo();
            connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
            connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
            connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
            connInfo.Password = ConfigurationManager.AppSettings["Password"];
            rep.Load(Server.MapPath("Reportes\\Ejecucion_Presupuestal_2012.rpt"), OpenReportMethod.OpenReportByTempCopy);
            CrystalReportViewer1.ReportSource = rep;
            Tables repTbls = rep.Database.Tables;

            //paramValue.Value = TraerFilasSeleccionadasDelGrid();
            //crtParamField.ParameterFieldName = "@PLN_ID";
            //crtParamField.CurrentValues.Add(paramValue);
            //crtParamFields.Add(crtParamField);

            foreach (Table repTbl in repTbls)
            {
                TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
                repTblLogonInfo.ConnectionInfo = connInfo;
                repTbl.ApplyLogOnInfo(repTblLogonInfo);
                repTbl.Location = "MCSIG" + ".dbo." + repTbl.Name;
            }
            Session["repPuntajeEstudiantesXPrograma"] = rep;
            CrystalReportViewer1.ReportSource = rep;
            CrystalReportViewer1.DataBind();
            //CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }
    }
}
